home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ov143b.zip / OVTAG.C < prev    next >
C/C++ Source or Header  |  1993-01-04  |  13KB  |  431 lines

  1. /*  024  28-May-87  ovtag.c
  2.  
  3.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include "ov.h"
  7. #include "overr.h"
  8. #include "dialog.h"
  9. #include "direct.h"
  10.  
  11. static char *name;                     /* wildcard (maybe) name to tag */
  12. static unsigned char attrib;           /* attribute to tag by */
  13. static unsigned from_date, to_date;    /* dates to use in tag by date */
  14. static unsigned from_time, to_time;    /* times to use in tag by time */
  15.  
  16. static D_BOX tag_box = { FIRST_NROW+3, 24, 8, 30, NULL, "Tag by Date/Time" };
  17.  
  18. /* define fields used by tag by date/time */
  19.  
  20. static D_FLD tdflds[] = {
  21.   { 3, 6, 8, 0, "        " }, { 3, 15, 8, 0, "00:00:00" }, { 3, 26, 1, 0, "A" },
  22.   { 5, 6, 8, 0, "        " }, { 5, 15, 8, 0, "12:00:00" }, { 5, 26, 1, 0, "P" } };
  23.  
  24. unsigned int cvtdate(), cvttime();
  25.  
  26. static struct _cvtstruct {     /* used to check/convert time/date fields */
  27.    unsigned *cvtresult;
  28.    int (*cvtrtn)();
  29.    char *name;
  30.    unsigned f1;
  31. } cvtflds[] = {
  32.    { &from_date, cvtdate, "from date", 0 },
  33.    { &from_time, cvttime, "from time", 1 },
  34.    { &to_date,   cvtdate, "to date",   3 },
  35.    { &to_time,   cvttime, "to time",   4 } };
  36.  
  37. int by_name(), by_date(), by_attrib();
  38.  
  39. extern WINDOW cw;
  40. extern FILE_ENT files[];
  41.  
  42. unsigned int getdate();
  43. char *strupr(), *mmddyy();
  44.  
  45.  
  46. /******************************************************************************
  47.  **                       T A G _ C U R R E N T                              **
  48.  *****************************************************************************/
  49.  
  50. tag_current() {        /* toggle the tag state of the current file */
  51.  
  52.    tag_toggle(&files[cw.curidx],cw.curidx);  /* toggle the current file */
  53.  
  54.    disp_file_stats();                  /* display (maybe) updated file stats */
  55. }
  56.  
  57.  
  58. /******************************************************************************
  59.  **                      T A G _ M O D I F I E D                             **
  60.  *****************************************************************************/
  61.  
  62. tag_modified() {       /* tag files with the archive attriute set */
  63.  
  64.    attrib = ARCHIVE;                   /* tag files with this attribute */
  65.  
  66.    tag_files(by_attrib);               /* tag'em an' flag'em */
  67. }
  68.  
  69.  
  70. /******************************************************************************
  71.  **                         T A G _ N A M E                                  **
  72.  *****************************************************************************/
  73.  
  74. tag_name() {           /* wildcard tag of files by name */
  75.  
  76.    name = strupr(prompt("Tag by name","File name pattern: ",
  77.           NULL,0,12));
  78.  
  79.    if (strlen(name) == 0)
  80.       return;
  81.  
  82.    tag_files(by_name);                         /* tag all matchting files */
  83. }
  84.  
  85.  
  86. /******************************************************************************
  87.  **                         T A G _ D A T E                                  **
  88.  *****************************************************************************/
  89.  
  90. tag_date() {
  91.  
  92.    int cvterr;
  93.    register int i;
  94.    char *tmp, *boxsave;
  95.    unsigned char quit = 0;
  96.    register struct _cvtstruct *cvp;
  97.  
  98.    /* display the tag by date dialog box */
  99.  
  100.    setvattrib(DIS_BOX);
  101.    dbx_open(&tag_box,DBX_SAVE);
  102.  
  103.    dbx_disp(&tag_box,"MM/DD/YY HH:MM:SS Am/Pm",1,6);   /* box text */
  104.    dbx_disp(&tag_box,"From",3,1);
  105.    dbx_disp(&tag_box,"To",5,1);
  106.  
  107.    setvattrib(DIS_HIBOX);
  108.    dbx_disp(&tag_box,"Press ESC to Quit",7,6);
  109.  
  110.    if (*tdflds[0].value == ' ') {         /* default to todays date */
  111.       tmp = mmddyy(getdate());
  112.       strcpy(tdflds[0].value,tmp);
  113.       strcpy(tdflds[3].value,tmp);
  114.    }
  115.  
  116.    /* display the input fields and default values */
  117.  
  118.    for (i = 0; i < 6; i++)
  119.       dbx_disp(&tag_box,tdflds[i].value,tdflds[i].row,tdflds[i].col);
  120.  
  121.    /* read the field values from user - dbx_rdfld also reads into a special
  122.       static buffer - copy it to local field value if something read        */
  123.  
  124.    for (i = 0; i < 6; i++) {
  125.       if (*(tmp = dbx_rdfld(&tag_box,&tdflds[i])))     /* read & test     */
  126.          strcpy(tdflds[i].value,tmp);                  /* save read value */
  127.       else {                                      /* quit if nothing read */
  128.          quit = 1;
  129.          break;
  130.       }
  131.    }
  132.  
  133.    setvattrib(DIS_NORM);
  134.  
  135.    /* convert ascii date/times to file date/time formats */
  136.  
  137.    if (!quit)
  138.       for (i = 0, cvp = cvtflds; i < 4; i++, cvp++) {
  139.          *cvp->cvtresult = (*cvp->cvtrtn)(tdflds[cvp->f1].value,
  140.                                           tdflds[cvp->f1+1].value,&cvterr);
  141.          if (cvterr) {
  142.             show_error(0,0,2,"Invalid ",cvp->name);
  143.             quit = 1;
  144.          }
  145.       }
  146.  
  147.    /* get rid of the dialog box */
  148.  
  149.    dbx_close(&tag_box);
  150.  
  151.    /* finally tag the files */
  152.  
  153.    if (!quit)
  154.       tag_files(by_date);
  155.  
  156. }
  157.  
  158.  
  159. /******************************************************************************
  160.  **                         T A G _ I N V E R T                              **
  161.  *****************************************************************************/
  162.  
  163. tag_invert() {         /* invert the tag state of all files */
  164.  
  165.    register int i;
  166.    register struct file_ent *fp;
  167.  
  168.    fp = files;
  169.    for (i = 0; i < cw.nfiles; i++, fp++)
  170.       tag_toggle(fp,i);
  171.  
  172.    disp_file_stats();                          /* disp updated stats */
  173.  
  174. }
  175.  
  176.  
  177. /******************************************************************************
  178.  **                         T A G _ R E S E T                                **
  179.  *****************************************************************************/
  180.  
  181. tag_reset() {          /* clear all tagged file indicators */
  182.  
  183.    register int i;
  184.    register struct file_ent *fp;
  185.  
  186.    fp = files;
  187.    for (i = 0; i < cw.nfiles; i++, fp++)
  188.       if (fp->flags & TAGGED) {
  189.          fp->flags &= ~TAGGED;
  190.          if (on_screen(i)) {
  191.             gotorc(idx2sr(i),idx2sc(i));
  192.             disp_file(fp,i == cw.curidx);
  193.          }
  194.       }
  195.  
  196.    cw.num_tagged = 0;                          /* no files tagged now */
  197.    cw.tag_size = 0;
  198.    disp_file_stats();                          /* disp updated (0) stats */
  199.  
  200. }
  201.  
  202.  
  203. /******************************************************************************
  204.  **                         T A G _ T O D A Y                                **
  205.  *****************************************************************************/
  206.  
  207. tag_today() {          /* tag all files created/modified today */
  208.  
  209.    from_date = to_date = getdate();
  210.    from_time = 0;
  211.    to_time = 0xffff;
  212.  
  213.    tag_files(by_date);
  214. }
  215.  
  216.  
  217. /******************************************************************************
  218.                           T A G _ F I N D
  219.  *****************************************************************************/
  220.  
  221. tag_find(dir)          /* find (goto) the next or perv tagged file */
  222. int dir;
  223. {
  224.    register int i;
  225.  
  226.    if (cw.num_tagged == 0)             /* nowhere to go if no files tagged */
  227.       return;
  228.  
  229.    /* find the next (prev) tagged file */
  230.  
  231.    i = cw.curidx;
  232.    do {
  233.       i += dir;                        /* move forward or backward */
  234.       if (i >= cw.nfiles)              /* gotta watch for wrap arounds */
  235.          i = 0;
  236.       else
  237.          if (i < 0)
  238.             i = cw.nfiles - 1;
  239.    } while ((files[i].flags & TAGGED) == 0);   /* done if at a tagged file */
  240.  
  241.    /* figure out how to move the file pointer to the tagged file */
  242.  
  243.    if (on_screen(i)) {
  244.       fp_off(cw.curidx);               /* if the new file is currently */
  245.       fp_on(cw.curidx = i);            /*   displayed, just move the pointer */
  246.    } else {
  247.       cw.curidx = i;                   /* otherwise ... */
  248.       adjust_window();                 /* update entire window if the */
  249.       update_window(1);                /*   file isn't displayed */
  250.    }
  251. }
  252.  
  253.  
  254. /******************************************************************************
  255.  **                       T A G _ T O G G L E                                **
  256.  *****************************************************************************/
  257.  
  258. static int
  259. tag_toggle(fp,i)       /* toggle the tag state of file pointed to by fp */
  260. register int i;
  261. register struct file_ent *fp;
  262. {
  263.  
  264.    if (!(fp->flags & DIR)) {
  265.       if ((fp->flags ^= TAGGED) & TAGGED) {    /* toggle tag state */
  266.          cw.num_tagged++;                      /* keep track of # tagged */
  267.          cw.tag_size += fp->size;              /* and tagged size */
  268.       } else {
  269.          cw.num_tagged--;
  270.          cw.tag_size -= fp->size;
  271.       }
  272.       if (on_screen(i)) {                      /* display file ent with */
  273.          gotorc(idx2sr(i),idx2sc(i));          /* new display attribute */
  274.          disp_file(fp,i == cw.curidx);
  275.       }
  276.    }
  277. }
  278.  
  279.  
  280. /******************************************************************************
  281.  **                         T A G _ F I L E S                                **
  282.  *****************************************************************************/
  283.  
  284. static int
  285. tag_files(cmpfunc)     /* tag all files matching criteria */
  286. int (*cmpfunc)();
  287. {
  288.  
  289.    register int i;
  290.    register struct file_ent *fp;
  291.    int last_tagged = cw.num_tagged;
  292.  
  293.    /* scan the files[] structure looking for files to tag.  Don't tag empty
  294.       files[] entries, directory entries, already tagged entries, or entries
  295.       that don't meet the qualifications (as determined by cmpfunc()).  When
  296.       an entry is tagged, redisplay it if its currently displayed, update
  297.       num_tagged count, and tagged file size.  Update file status display
  298.       when done. */
  299.  
  300.    fp = files;
  301.    for (i = 0; i < cw.nfiles; i++, fp++)
  302.       if (!(fp->flags & DIR) && !(fp->flags & TAGGED) && (*cmpfunc)(fp)) {
  303.          fp->flags |= TAGGED;
  304.          if (on_screen(i)) {
  305.             gotorc(idx2sr(i),idx2sc(i));
  306.             disp_file(fp,i == cw.curidx);
  307.          }
  308.          cw.num_tagged++;
  309.          cw.tag_size += fp->size;
  310.       }
  311.  
  312.    if (cw.num_tagged != last_tagged)           /* actually tag any? */
  313.       disp_file_stats();                       /* disp updated stats */
  314. }
  315.  
  316.  
  317. /******************************************************************************
  318.  **                         B Y _ A T T R I B                                **
  319.  *****************************************************************************/
  320.  
  321. static int
  322. by_attrib(fp)          /* determine if file should be tagged by attribute */
  323. struct file_ent *fp;
  324. {
  325.    return(fp->flags & attrib);
  326. }
  327.  
  328.  
  329. /******************************************************************************
  330.  **                         B Y _ N A M E                                    **
  331.  *****************************************************************************/
  332.  
  333. static int
  334. by_name(fp)            /* determine if file entry should be tagged by name */
  335. struct file_ent *fp;
  336. {
  337.    return(match_name(fp->name,name));
  338. }
  339.  
  340.  
  341. /******************************************************************************
  342.  **                         B Y _ D A T E                                    **
  343.  *****************************************************************************/
  344.  
  345. static int
  346. by_date(fp)            /* determine if file entry should be tagged by date */
  347. register struct file_ent *fp;
  348. {
  349.    if ((fp->date > from_date ||
  350.        (fp->date == from_date && fp->time >= from_time)) &&
  351.        (fp->date < to_date   ||
  352.        (fp->date == to_date && fp->time <= to_time)))
  353.       return(1);
  354.    else
  355.       return(0);
  356. }
  357.  
  358.  
  359. /*****************************************************************************
  360.                         C V T D A T E / T I M E
  361.  *****************************************************************************/
  362.  
  363. static unsigned int
  364. cvtdate(date,dummy,ep) /* convert ascii date to integer file fmt */
  365. char *date, *dummy;
  366. int *ep;
  367. {
  368.    int month, day, year;
  369.  
  370.    *ep = 0;                            /* assume no error */
  371.  
  372.    month = cvttwo(date);               /* cvt flds to binary */
  373.    day   = cvttwo(date+3);
  374.    year  = cvttwo(date+6);
  375.  
  376.    /* error check */
  377.  
  378.    if (month < 1 || month > 12 || day < 1 || day > 31 || year < 80 ||
  379.        year > 99 || date[2] != '/' || date[5] != '/') {
  380.       *ep = 1;
  381.       return(0);
  382.    }
  383.  
  384.    /* turn it into the integer format */
  385.  
  386.    return( ((year - 80) << 9) + (month << 5) + day );
  387. }
  388.  
  389.  
  390. static unsigned int
  391. cvttime(time,ampmp,ep) /* convert ascii time to integer file fmt */
  392. char *time, *ampmp;
  393. int *ep;
  394. {
  395.    unsigned int hh, mm, ss, ampm;
  396.  
  397.    *ep = 0;                            /* assume no error */
  398.  
  399.    hh = cvttwo(time);                  /* cvt flds to binary */
  400.    mm = cvttwo(time+3);
  401.    ss = cvttwo(time+6);
  402.  
  403.    ampm = toupper(*ampmp);             /* make sure its upper case */
  404.  
  405.    /* error check */
  406.  
  407.    if (hh > 12 || mm > 59 || ss > 59 || time[2] != ':' || time[5] != ':' ||
  408.        (ampm != 'A' &&  ampm != 'P')) {
  409.       *ep = 1;
  410.       return(0);
  411.    }
  412.  
  413.    /* turn it into the integer format */
  414.  
  415.    if (ampm == 'P')
  416.       hh += 12;
  417.  
  418.    return( (hh << 11) + (mm << 5) + (ss >> 1) );
  419. }
  420.  
  421.  
  422. static int
  423. cvttwo(cp)             /* convert 2 ascii chars to binary */
  424. register char *cp;
  425. {
  426.    if (*cp == ' ')             /* it may have a leading blank */
  427.       *cp = '0';
  428.  
  429.    return( ((*cp - '0') * 10) + (cp[1] - '0') );
  430. }
  431.